//
//  DB_Collect.m
//  LPSNews
//
//  Created by Ruan on 14-10-21.
//  Copyright (c) 2014年 LPS. All rights reserved.
//

#import "DB_Collect.h"

@implementation DB_Collect


- (void)dealloc
{
    
    [_collectData release];
    [super dealloc];
}


- (void)openCollectDB
{
    
    // 1.获得收藏数据库的路径
    NSString *collectordbPath = [FilePathManager collectorfilePath];
    
    // 2.fmdb 创建数据库队列
    FMDatabaseQueue *fmdbQueue = [FMDatabaseQueue databaseQueueWithPath:collectordbPath];
    self.fmdbQueue = fmdbQueue;
    // 3.打开数据库创建表格
    [fmdbQueue inDatabase:^(FMDatabase *db) {

        int result = [db executeUpdate:@"create table if not exists t_collect  (id integer primary key autoincrement, docid text,title text, ptime text, body text,imagecount integer);"];
        if (result) {
           
        }else
        {
            
        }
        
    }];
}

- (void)insertCollectDataWithDocid:(NSString *)docid Title:(NSString *)title Ptime:(NSString *)ptime Body:(NSString *)body Imagecount:(NSNumber *)imagecount
{
    [self openCollectDB];
    
    if ([self collectDataisExistsDocid:docid]) {
        return;
    }
    
    [self.fmdbQueue inDatabase:^(FMDatabase *db) {
        
        int result = [db executeUpdate:@"insert into t_collect (docid, title, ptime, body,imagecount) values (?,?,?,?,?)",docid,title,ptime,body,imagecount];
        if (result) {
         
        }else
        {
            
        }
        
    }];
}


- (BOOL)collectDataisExistsDocid:(NSString *)docid
{
    
    [self openCollectDB];
    
    __block NSString *str = nil;
    
    [self.fmdbQueue inDatabase:^(FMDatabase *db) {
        // 1. 结果集 查询语句
        FMResultSet *rt = [db executeQuery:@"select *from t_collect where docid = ?",docid];
        // 2.查询数据
        while ([rt next]) {
            str = [rt stringForColumn:@"docid"];
        }
    }];

    if (str) {
        return YES;
    }

    return NO;
}



- (CollectDataMode *)selectCollectDataWithDocid:(NSString *)docid

{
    [self openCollectDB];
    CollectDataMode *collectData = [[CollectDataMode alloc] init];
    
    [self.fmdbQueue inDatabase:^(FMDatabase *db) {
        // 1. 结果集 查询语句
        FMResultSet *rt = [db executeQuery:@"select *from t_collect where docid = ?",docid];
        // 2.查询数据
        while ([rt next]) {
            
            collectData.docid = [rt stringForColumn:@"docid"];
            collectData.title = [rt stringForColumn:@"title"];
            collectData.ptime = [rt stringForColumn:@"ptime"];
            collectData.body = [rt stringForColumn:@"body"];
            collectData.imagecount = [rt intForColumn:@"imagecount"];
            
        }
    }];
    return collectData;
}



- (NSMutableArray *)selectAllCollectData

{
    [self openCollectDB];
    NSMutableArray *collectDataArray = [[NSMutableArray alloc] init];

    [self.fmdbQueue inDatabase:^(FMDatabase *db) {
        // 1. 结果集 查询语句
        FMResultSet *rt = [db executeQuery:@"select *from t_collect"];
        // 2.查询数据
        while ([rt next]) {
            CollectDataMode *collectData = [[CollectDataMode alloc] init];
            collectData.docid = [rt stringForColumn:@"docid"];
            collectData.title = [rt stringForColumn:@"title"];
            collectData.ptime = [rt stringForColumn:@"ptime"];
            collectData.body = [rt stringForColumn:@"body"];
            self.collectData.imagecount = [rt intForColumn:@"imagecount"];
            [collectDataArray addObject:collectData];
            [collectData release];
        }
        
    }];
    return collectDataArray;
}

- (void)deleteCollectDataWithDocid:(NSString *)docid
{
    
    [self openCollectDB];
    
    [self.fmdbQueue inDatabase:^(FMDatabase *db) {
        
        int result = [db executeUpdate:@"delete from t_collect where docid = ?",docid];
        if (result) {
         
        }else
        {
            
        }
        
    }];
}

@end
